{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "422f2d8e",
   "metadata": {},
   "source": [
    "----------\n",
    "* 记录人：简钰英\n",
    "* 时间：2021-11-18\n",
    "* week12\n",
    "* 主要参考文档:\n",
    "> 1. [Python官方文档](https://python/org)\n",
    "> 2. [gitee课堂笔记](https://gitee.com/xzhichao/python)\n",
    "\n",
    "----------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94b21ced",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "## 1. 字典的实践（一）\n",
    "\n",
    "* 课本P112-P113\n",
    "* 知识点：\n",
    "> 1. 利用字典计算词频\n",
    "> 2. 字典的初始化\n",
    "\n",
    "```\n",
    "# -*- coding:utf-8 -*-\n",
    "# Author:Zhichao\n",
    "\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "found = found.fromkeys(vowels,0)\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "\n",
    "# print(found.items())\n",
    "for k,v in found.items():\n",
    "    print(k,\"was found\",v,\"time(s)\")\n",
    "# print(found)\n",
    "\n",
    "vowels4\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "05d70712",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一段文本，将统计元音字母出现的次数:hello zhichao\n",
      "{'a': 1, 'e': 1, 'i': 1, 'o': 2, 'u': 0}\n"
     ]
    }
   ],
   "source": [
    "# 在列表基础上（列表可统计出现的内容有哪些）\n",
    "# 字典可统计出现的内容 + 出现的次数（词频）\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"] # iterable\n",
    "word = input(\"请输入一段文本，将统计元音字母出现的次数:\")\n",
    "found = {}\n",
    "\n",
    "# 利用字典创建的方式，创建字典: dict_name[key]=value\n",
    "# found[\"a\"]=0\n",
    "# found[\"e\"]=0\n",
    "# found[\"i\"]=0\n",
    "# found[\"o\"]=0\n",
    "# found[\"u\"]=0\n",
    "\n",
    "# 方法一：循环 vowels\n",
    "# for i in vowels:   # 可优化  fromkeys()\n",
    "#     found[i] = 0\n",
    "# 方法二：利用fromkeys 实现初始化值\n",
    "found = found.fromkeys(vowels,0)\n",
    "    \n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "edf8f017",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}\n"
     ]
    }
   ],
   "source": [
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32c0d0ec",
   "metadata": {},
   "source": [
    "> 1. 字典的高级用法：fromkeys()\n",
    ">> 1. 给定一个序列数据作为key\n",
    ">> 2. 把序列<font style=\"color:red\">**所有**</font>内容进行初始化,作为新的字典内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "da06cc34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "550025c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_list = ['Alex','Marry','Jack','Jan'] # iterable\n",
    "names_age_dict = {}\n",
    "names_age_dict = names_age_dict.fromkeys(names_list,0) # 把一个序列作为一个字典的 keys ，然后初始化其值（默认值为：None），可以给定一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "080c31ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Alex': 0, 'Marry': 0, 'Jack': 0, 'Jan': 0}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_age_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8a49b09",
   "metadata": {},
   "source": [
    "## 2. 字典的实践（二）\n",
    "* 课本P121\n",
    "\n",
    "```\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)  #如果是元音字母，就在字典found中初始化这个元音字母，值为0\n",
    "        found[letter] +=1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,v)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8462702f",
   "metadata": {},
   "source": [
    "> 2. 字典的高级用法 setdefault()\n",
    ">> 1. 如果字典中还没有该内容（item） ，新增该item作为字典的key，默认值default = None，可以指定值的内容(初始化值的内容)\n",
    ">> 2. 如果 该内容（item） 已经是字典的key， 则不操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "97a2aba6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names_age_dict.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "69757b44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {'a':2}\n",
    "found.setdefault('e',0) # e是不存在 found中的\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6c225ecf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found.setdefault('a',5) # a已经存在在 字典found的中了，不操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e9ca7313",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 2, 'e': 0}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6223d5fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello xiaoming\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'e': 1, 'o': 2, 'i': 2, 'a': 1}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 课本字典实践二：\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)  \n",
    "        found[letter] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcabc0ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 步骤：\n",
    "# h 不在\n",
    "# e found ={'e':1}\n",
    "# l 不在\n",
    "# l 不在\n",
    "# o found ={'e':1,'o':1}\n",
    "#   不在\n",
    "# x 不在\n",
    "# i found ={'e':1,'o':1,'i':1}\n",
    "# a found ={'e':1,'o':1,'i':1,'a':1}\n",
    "# o found = {'e':1,'o':2,'i':1,'a':1}\n",
    "# m 不在\n",
    "# i found = {'e':1,'o':2,'i':2,'a':1}\n",
    "# n 不在\n",
    "# g 不在"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
